// tu_mutex.h	-- Vitaly Alexeev <tishka92@yahoo.com>	2007

// This source code has been donated to the Public Domain.  Do
// whatever you want with it.

#ifndef GAMESWF_MUTEX_H
#define GAMESWF_MUTEX_H

#include "base/tu_config.h"
#include "gameswf/gameswf_types.h"
#include "gameswf/gameswf_log.h"

typedef void (*thread_start_func)(void* arg);


namespace gameswf
{
	typedef void (*thread_proc)(void*);

	class tu_thread : public ref_counted
	{
	public:
		tu_thread() {}
		tu_thread(thread_proc fn, void* data) {}
		virtual ~tu_thread() {}

		virtual void wait() = 0;
		virtual void kill() = 0;
		virtual void start() = 0;
	};

	class tu_mutex
	{
	public:
		tu_mutex() {}
		virtual ~tu_mutex() {}

		virtual void lock() = 0;
		virtual void unlock() = 0;
	};

	class tu_condition
	{
	public:
		tu_condition() {}
		virtual ~tu_condition() {}

		virtual void wait() = 0;
		virtual void signal() = 0;
	};

	class tu_autolock
	{
	public:
		exported_module tu_autolock(tu_mutex& mutex) :
		m_mutex(mutex)
		{
			m_mutex.lock();
		}

		exported_module ~tu_autolock()
		{
			m_mutex.unlock();
		}

	protected:
		tu_mutex& m_mutex;
	};

	typedef tu_thread* (*create_thread_func)(thread_proc, void*);
	typedef tu_mutex* (*create_mutex_func)();
	typedef tu_condition* (*create_condition_func)();

	exported_module tu_thread* createThread(thread_proc fn, void* data);
	exported_module tu_mutex* createMutex();
	exported_module tu_condition* createCondition();

	exported_module void setCreateThreadFunc(create_thread_func func);
	exported_module void setCreateMutexFunc(create_mutex_func func);
	exported_module void setCreateConditionFunc(create_condition_func func);
}

#endif	// GAMESWF_MUTEX_H
